﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.CMS.RegAssembly;
using System.Text.RegularExpressions;
using TRPOP.Common;
using System.Collections;
namespace TRPOP.CMS.Template.GenerateCS
{

    public partial class ConvertTags
    {
        /// <summary>
        /// 解析   [$var] [$var.var] [$var.0.var]
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <returns></returns>
        public static string ConvertTag_Var(ref string strSource, Match oMatch, string strSpaces, bool IsAddNote)
        {
            string strReturn = string.Empty;
            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            // 解析 [$var] [$var.var] [$var.0.var]  [$var.1.var] ......
            strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (ConvertSpecialChar(GetTagValue(\"{0}\", \"{1}\", \"{2}\")));",
                                                    oMatch.Groups["DataSet"].Value.Trim(),
                                                    oMatch.Groups["index"].Value.Trim(),
                                                    oMatch.Groups["var"].Value.Trim());
            if (IsAddNote)
            {
                strReturn += string.Format(strTagAnaylisenote, oMatch.Value, Environment.NewLine);//模板解析的注释代码
            }
            else
                strReturn += Environment.NewLine;

            strSource = strSource.Substring(oMatch.Index + oMatch.Value.Length);
            return strReturn;
        }

        /// <summary>
        /// 解析 <!--  <var name="cfg_webname" value="约克软件" /> --> 
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <param name="IsAddNote"></param>
        /// <returns></returns>
        public static string ConvertTag_DefineVar(ref string strSource, Match oMatch, string strSpaces, bool IsAddNote)
        {
            string strReturn = string.Empty;

            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            //解析 <var name="cfg_webname" value="约克软件" />
            bool blnIsRightGrammar = false;
            Match oVarMatch = oTags_Var.Match(strSource, oMatch.Index);
            if (oVarMatch.Success)
            {
                Dictionary<string, string> oDictionary = GetAttribute(oVarMatch.Groups["attribute"].ToString());
                if (oDictionary.ContainsKey("name") && oDictionary.ContainsKey("value"))
                {
                    blnIsRightGrammar = true;
                    strReturn += strSpaces +
                                 string.Format("SaveVar(\"{0}\", \"{1}\");",
                                 CommonMethord.ConvertSpecialChar(oDictionary["name"]),
                                 CommonMethord.ConvertSpecialChar(oDictionary["value"])
                                 );
                    if (IsAddNote)
                    {
                        strReturn += string.Format(strTagAnaylisenote, oVarMatch.Value, Environment.NewLine);//模板解析的注释代码
                    }
                    else
                        strReturn += Environment.NewLine;
                }
            }
            if (blnIsRightGrammar == false)//如果语法错误
            {
                strSource = strSource.Replace(oMatch.Value, TagsError.GetTagsError(TagsError.TagsErrorAnalysis.ArrtibuteError));
            }
            else
            {
                strSource = strSource.Replace(oVarMatch.Value, "");
            }

            if (!strSource.IsEmpty() && strSource.Length > oMatch.Index + 1)
            {
                strSource = strSource.Substring(oMatch.Index);
            }
            return strReturn;
        }

        /// <summary>
        /// 解析 
        /// <!--  <op exp=' int test = 12;   string strTemp = "1"; SaveVar("strTemp" , strTemp); ' /> --> 这样[$intTemp]可以使用
        /// <!--  <op exp=' test =  test+14;  ' />  -->  这样[$test] 使用不了的
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <param name="IsAddNote"></param>
        /// <returns></returns>
        public static string ConvertTag_OP(ref string strSource, Match oMatch, string strSpaces, bool IsAddNote)
        {
            string strReturn = string.Empty;

            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            //解析 <op exp='......' />
            bool blnIsRightGrammar = false;
            Match oVarMatch = oTags_Op.Match(strSource, oMatch.Index);
            if (oVarMatch.Success)
            {
                Dictionary<string, string> oDictionary = GetAttributeByGingleQuotes(oVarMatch.Groups["attribute"].ToString());
                if (oDictionary.ContainsKey("exp"))
                {
                    blnIsRightGrammar = true;
                    strReturn += strSpaces + oDictionary["exp"];
                    if (IsAddNote)
                    {
                        strReturn += string.Format(strTagAnaylisenote, oVarMatch.Value, Environment.NewLine);//模板解析的注释代码
                    }
                    else
                        strReturn += Environment.NewLine;

                }
            }

            if (blnIsRightGrammar == false)//如果语法错误
            {
                strSource = strSource.Replace(oMatch.Value, TagsError.GetTagsError(TagsError.TagsErrorAnalysis.ArrtibuteError));
            }
            else
            {
                strSource = strSource.Replace(oVarMatch.Value, "");
            }

            if (!strSource.IsEmpty() && strSource.Length > oMatch.Index + 1)
            {
                strSource = strSource.Substring(oMatch.Index);
            }
            return strReturn;
        }
    }
}